package com.zx.notification.common.mapstruct;

import com.zx.core.enums.StatusEnum;
import com.zx.notification.common.enums.BizTypeEnum;
import com.zx.notification.common.enums.ChannelTypeEnum;
import com.zx.notification.common.enums.ConsumeModeEnum;
import com.zx.notification.common.model.NotificationTemplates;
import com.zx.notification.common.param.NotificationTemplatesParam;
import com.zx.notification.common.vo.NotificationTemplatesVO;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * <p>
 * description: 通知模板映射器 <br>
 * create: 2025-10-15
 * </p>
 *
 * @author zhou  xun
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface NotificationTemplatesMapStructMapper {

    /**
     * 将NotificationTemplatesParam转换为NotificationTemplates实体
     *
     * @param notificationTemplatesParam 通知模板参数
     * @return NotificationTemplates实体
     */
    NotificationTemplates notificationTemplatesParamToNotificationTemplates(NotificationTemplatesParam notificationTemplatesParam);

    /**
     * 将NotificationTemplates实体转换为NotificationTemplatesVO视图对象
     *
     * @param notificationTemplates 通知模板实体
     * @return NotificationTemplatesVO视图对象
     */
    NotificationTemplatesVO notificationTemplatesToNotificationTemplatesVO(NotificationTemplates notificationTemplates);

    /**
     * 在映射完成后，设置描述性字段
     *
     * @param notificationTemplates   通知模板实体
     * @param notificationTemplatesVO 通知模板VO
     */
    @AfterMapping
    default void setDescriptions(NotificationTemplates notificationTemplates, @MappingTarget NotificationTemplatesVO notificationTemplatesVO) {
        // 设置业务类型描述
        BizTypeEnum bizTypeEnum = BizTypeEnum.getEnumByCode(notificationTemplates.getBizType());
        if (bizTypeEnum != null) {
            notificationTemplatesVO.setBizTypeDesc(bizTypeEnum.getDesc());
        }

        // 设置支持渠道描述 (处理逗号分隔的多个渠道)
        String supportedChannels = notificationTemplates.getSupportedChannels();
        if (supportedChannels != null && !supportedChannels.isEmpty()) {
            String supportedChannelsDesc = Arrays.stream(supportedChannels.split(","))
                    .map(String::trim)
                    .map(ChannelTypeEnum::getEnumByCode)
                    .filter(java.util.Objects::nonNull)
                    .map(ChannelTypeEnum::getDesc)
                    .collect(Collectors.joining(", "));
            notificationTemplatesVO.setSupportedChannelsDesc(supportedChannelsDesc);
        }

        // 设置消费模式描述
        ConsumeModeEnum consumeModeEnum = ConsumeModeEnum.getEnumByCode(notificationTemplates.getConsumeMode());
        if (consumeModeEnum != null) {
            notificationTemplatesVO.setConsumeModeDesc(consumeModeEnum.getDesc());
        }

        // 设置状态描述
        StatusEnum statusEnum = StatusEnum.getEnumByCode(notificationTemplates.getStatus());
        if (statusEnum != null) {
            notificationTemplatesVO.setStatusDesc(statusEnum.getDesc());
        }
    }
}